package com.wxp.alice.lanfilemaster.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

public class PermissionUtils {
    /**
     * check if the specified permission is granted
     *
     * @param context 上下文
     * @param permission permission name
     * @return true if the permission is granted
     */
    public static boolean hasPermissionGranted(Context context, String permission) {
        return ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * check if all specified permissions are granted
     *
     * @param context 上下文
     * @param permissions permissions
     * @return true if all permissions are granted
     */
    public static boolean hasPermissionsGranted(Context context, String[] permissions) {
        for (String permission : permissions) {
            if (!hasPermissionGranted(context, permission)) {
                return false;
            }
        }
        return true;
    }

    /**
     * request the specified permissions
     *
     * @param activity activity
     */
    public static void requestPermissions(Activity activity, String[] permissions) {
        ActivityCompat.requestPermissions(activity, permissions, 1);
    }
}
